<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection" :dataSource="dataSource">
      <template #tableTitle>
        <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleAdd">新增</a-button>
      </template>
      <template #executeCount="{ record }">
        <a @click="handleRunChange(record)">{{ record.enable ? '已启动' : '已停止' }}</a>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" />
      </template>
    </BasicTable>
    <ScriptModal @register="registerModal" @success="reload" />
    <a-modal title="提示" v-model:open="runVisible" centered @ok="handleRunOk">
      <p class="modal-content">确定{{ runModals.content.enable ? '禁用' : '启动' }}该脚本吗？</p>
    </a-modal>
  </div>
</template>
<script lang="ts" name="script-list" setup>
  import { ref, reactive } from 'vue';
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { list, runScript, deleteScript } from './script.api';
  import { columns, searchFormSchema } from './script.data';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { useMessage } from '/@/hooks/web/useMessage';
  import ScriptModal from './ScriptModal.vue';
  const [registerModal, { openModal }] = useModal();
  const { createMessage: $message } = useMessage();
  const runVisible = ref<boolean>(false);
  const runModals = reactive({ content: {} });
  // 列表页面公共参数、方法
  const { tableContext } = useListPage({
    tableProps: {
      title: '脚本管理',
      //   api: list,
      columns: columns,
      showIndexColumn: true,
      formConfig: {
        schemas: searchFormSchema,
      },
    },
  });

  const [registerTable, { reload }] = tableContext;

  const dataSource = reactive([
    {
      createTime: '2024-03-04 15:18:28',
      desc: '描述',
      enable: true,
      key: 'f8f8e1d391664ba18bbb471d0e1d9522',
      matchTopic: 'test1',
      script: ' function convert(topic, payload){\n   return payload;\n}',
      scriptType: 'JAVASCRIPT',
    },
    {
      createTime: '2024-02-20 16:02:33',
      enable: false,
      key: 'b76bcb010613469f915908ec30a5739c',
      matchTopic: 'test',
      script: ' function convert(topic, payload){\n   return payload;\n}',
      scriptType: 'JAVASCRIPT',
    },
  ]);

  /**
   * 操作列定义
   * @param record
   */
  function getActions(record) {
    return [
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
        ifShow: () => {
          return !record.enable;
        },
      },
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        },
        ifShow: () => {
          return !record.enable;
        },
      },
      {
        label: '查看',
        onClick: handleDetail.bind(null, record),
        ifShow: () => {
          return record.enable;
        },
      },
    ];
  }

  /**
   * 新增事件
   */
  function handleAdd() {
    openModal(true, {
      isUpdate: false,
      showFooter: true,
    });
  }

  /**
   * 编辑事件
   */
  async function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
      showFooter: true,
    });
  }

  /**
   * 查看事件
   */
  async function handleDetail(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
      showFooter: false,
    });
  }

  /**
   * 删除事件
   */
  async function handleDelete(record) {
    // await deleteScript({ id: record.id }, reload);
  }

  function handleRunChange(record) {
    runModals.content = record;
    runVisible.value = true;
  }

  async function handleRunOk() {
    let params: any = {};
    params.key = runModals.content.key;
    params.state = !runModals.content.enable;
    await runScript(params, reload);
  }
</script>
<style scoped>
  .modal-content {
    height: 80px;
    line-height: 80px;
    padding-left: 30px;
    font-size: 15px;
  }
</style>
